<template>
	<view>
		<!-- 使用自定义的搜索组件 -->
		<view class="search-box">
		  <my-search @click.native="gotoSearch"></my-search>
		</view>
		<!-- 轮播图 开始 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
			<swiper-item v-for="(item,index) in swiperList" :key="index">
				<navigator :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
					<image :src="item.image_src" ></image>
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 轮播图 结束 -->
		<!-- 导航 开始 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item, i) in navList" :key="i" @click="handleClickNav(item)">
			 <image :src="item.image_src" class="nav-img" mode="widthFix"></image>
		   </view>
		</view>
		<!-- 导航 结束 -->
		<!-- 楼层开始 -->
		<view class="list">
			<view class="item" v-for="(item,index) in floorList" :key="index">
				<image :src="item.floor_title.image_src" mode="widthFix"></image>
				<view class="product_area">
					<navigator class="left" :url="item.product_list[0].navigator_url">
						<image :src="item.product_list[0].image_src" mode="widthFix" :style="{width:item.product_list[0].image_width+'rpx'}"></image>
					</navigator>
					<view class="right" >
						<navigator v-for="(item1,index1) in filterList(item.product_list)" :key="index1" :url="item1.navigator_url">
							<image :src="item1.image_src" mode="widthFix" :style="{width:item1.image_width+'rpx'}"></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
		<!-- 楼层结束 -->
	</view>
</template>

<script>
	// 导入自己封装的 mixin 模块
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins:[badgeMix],
		onLoad() {
			this.getSwiperList()
			this.getNavList()
			this.getFloorList()
		},
		methods: {
			async getSwiperList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/swiperdata')
				// 3.2 请求失败
				if (res.meta.status !== 200) return uni.$showMsg()
				// 3.3 请求成功，为 data 中的数据赋值
				this.swiperList = res.message
			},
			// 3. 在 methods 中定义获取数据的方法
			async getNavList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/catitems')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.navList = res.message
			},
			handleClickNav(obj){
				if(obj.name === '分类'){
					uni.switchTab({
						url:'/pages/cate/cate'
					})
				}
			},
			// 3. 定义获取楼层列表数据的方法
			    async getFloorList() {
			      const { data: res } = await uni.$http.get('/api/public/v1/home/floordata')
			      if (res.meta.status !== 200) return uni.$showMsg()
				  res.message.forEach((item,index)=>{
					  item.product_list.forEach((item1,index1)=>{
						  item1.navigator_url = '/subpkg/goods_list/goods_list?'+item1.navigator_url.split('?')[1]
					  })
				  })
			      this.floorList = res.message
			    },
			filterList(list){
				return list.slice(1)
			},
			gotoSearch() {
			  uni.navigateTo({
			    url: '/subpkg/search/search/search'
			  })
			}
		},
		data() {
			return {
				swiperList: [],
				navList: [],
				floorList:[]
			};
		}
	}
</script>

<style lang="scss">
	swiper {
		width: 750rpx;

		image {
			width: 750rpx;
			height: 330rpx;
		}
	}
	.nav-list{
		display: flex;
		margin: 15px 0;
		.nav-item{
			flex:1;
			image{
				width:100%;
			}
		}
	}
	.product_area{
		display: flex;
		justify-content: space-between;
		.right{
			display: flex;
			flex-wrap: wrap;
			justify-content:space-around;
		}
	}
	.search-box {
	  // 设置定位效果为“吸顶”
	  position: sticky;
	  // 吸顶的“位置”
	  top: 0;
	  // 提高层级，防止被轮播图覆盖
	  z-index: 999;
	}
</style>
